package cn.stylefeng.guns.modular.project.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.bean.BetweenMonth;
import cn.stylefeng.guns.comm.util.BetweenTimeUtil;
import cn.stylefeng.guns.modular.project.entity.Project;
import cn.stylefeng.guns.modular.project.entity.ProjectMember;
import cn.stylefeng.guns.modular.project.entity.ProjectMemberWork;
import cn.stylefeng.guns.modular.project.model.params.ProjectMemberParam;
import cn.stylefeng.guns.modular.project.model.params.ProjectMemberWorkParam;
import cn.stylefeng.guns.modular.project.model.result.ProjectMemberResult;
import cn.stylefeng.guns.modular.project.model.result.ProjectMemberWorkResult;
import cn.stylefeng.guns.modular.project.service.ProjectMemberService;
import cn.stylefeng.guns.modular.project.service.ProjectMemberWorkService;
import cn.stylefeng.guns.modular.project.service.ProjectService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 控制器
 *
 * @author
 * @Date 2019-10-21 11:22:42
 */
@Controller
@RequestMapping("/project/member/work")
public class ProjectMemberWorkController extends BaseController {

    private String PREFIX = "/project";

    @Autowired
    private ProjectMemberWorkService projectMemberWorkService;
    @Autowired
    private ProjectMemberService projectMemberService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("")
    public String index(int projectId, Long userId) {
        ProjectMemberParam param = new ProjectMemberParam();
        param.setProjectId(projectId);
        List<ProjectMemberResult> projectMembers = projectMemberService.findListBySpec(param);

        setAttr("projectMembers", projectMembers);
        setAttr("projectId", projectId);
        setAttr("userId", userId == null ? "-1" : userId);
        return PREFIX + "/projectMemberWork.html";
    }

    /**
     * 跳转到员工工时上报页面
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/userself")
    public String userself() {

        // 如果是项目经理可查看所有人员信息
        if (ShiroKit.hasAnyRoles("XMJL,administrator")) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
            queryWrapper.orderByAsc("name");
            List<User> list = userService.list(queryWrapper);
            setAttr("users", list);
            //setAttr("userId", ShiroKit.getUser().getId());

            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.orderByAsc("name");
            List<Project> list1 = projectService.list(projectQueryWrapper);
            setAttr("myProjects", list1);
        } else {
            List<ProjectMemberResult> myProjects = projectMemberService.findMyProjects();
            setAttr("myProjects", myProjects);
        }
        return PREFIX + "/projectMemberWork_user_self.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/add")
    public String add(int projectId) {
        setAttr("projectId", projectId);

        QueryWrapper<ProjectMember> queryWrapper = new QueryWrapper<ProjectMember>();
        queryWrapper.eq("project_id", projectId);
        List<ProjectMember> list = projectMemberService.list(queryWrapper);

        ArrayList<ProjectMemberWorkResult> projectMemberWorkResultArrayList = new ArrayList<ProjectMemberWorkResult>();
        for (ProjectMember projectMember : list) {
            ProjectMemberWorkResult projectMemberWorkResult = new ProjectMemberWorkResult();
            User user = userService.getOne(new QueryWrapper<User>().eq("user_id", projectMember.getUserId()));
            projectMemberWorkResult.setUserId(user.getUserId());
            projectMemberWorkResult.setName(user.getName());
            projectMemberWorkResultArrayList.add(projectMemberWorkResult);
        }
        setAttr("users", projectMemberWorkResultArrayList);

        setAttr("current", DateUtil.formatDate(new Date()));

        return PREFIX + "/projectMemberWork_add.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/add/userself")
    public String addUserSelf() {

        List<ProjectMemberResult> myProjects = projectMemberService.findMyProjects();
        setAttr("myProjects", myProjects);
        setAttr("current", DateUtil.formatDate(new Date()));

        return PREFIX + "/projectMemberWork_add_user_self.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/projectMemberWork_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ProjectMemberWorkParam projectMemberWorkParam) {
//        if(ToolUtil.isEmpty(projectMemberWorkParam.getUserIds())){
//            return ResponseData.error("请选择项目组成员。");
//        }

        for (String userId : projectMemberWorkParam.getUserIds()) {
            if (ToolUtil.isNotEmpty(userId)) {
                projectMemberWorkParam.setUserId(Long.valueOf(userId));
                this.projectMemberWorkService.add(projectMemberWorkParam);
            }
        }
        return ResponseData.success();
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/addItem/userself")
    @ResponseBody
    public ResponseData addItemUserSelf(ProjectMemberWorkParam projectMemberWorkParam) {
        projectMemberWorkService.addWorkHourSelf(projectMemberWorkParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ProjectMemberWorkParam projectMemberWorkParam) {
        this.projectMemberWorkService.update(projectMemberWorkParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ProjectMemberWorkParam projectMemberWorkParam) {
        this.projectMemberWorkService.delete(projectMemberWorkParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-10-21
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ProjectMemberWorkParam projectMemberWorkParam) {
        ProjectMemberWork detail = this.projectMemberWorkService.getById(projectMemberWorkParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-10-21
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ProjectMemberWorkParam projectMemberWorkParam) {
        return this.projectMemberWorkService.findPageBySpec(projectMemberWorkParam);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-10-21
     */
    @ResponseBody
    @RequestMapping("/list/userself")
    public LayuiPageInfo listUserSelf(ProjectMemberWorkParam projectMemberWorkParam, String betweenDayStr, Long userId) {
        if (betweenDayStr != null && betweenDayStr.length() > 0) {
            BetweenMonth betweenMonthBean = BetweenTimeUtil.getBetweenMonth(betweenDayStr, "至");
            projectMemberWorkParam.setBetweenMonth(betweenMonthBean);
        }        // 如果是项目经理可查看所有人员信息
        if (ShiroKit.hasAnyRoles("XMJL,administrator")) {
            if (userId != null) {
                projectMemberWorkParam.setUserId(userId);
            } else {
                //projectMemberWorkParam.setUserId(ShiroKit.getUser().getId());
            }
        } else {
            projectMemberWorkParam.setUserId(ShiroKit.getUser().getId());
        }
        return this.projectMemberWorkService.findPageBySpec(projectMemberWorkParam);
    }

}


